//
// Created by jinling on 2025/6/24.
//

#include "StringUtils.h"

//s 以sub开头
bool tmms::base::StringUtils::StarsWith(const std::string& s, const std::string& sub)
{
    if (sub.empty())
    {
        return true ;
    }
    if (s.empty())
    {
        return false;
    }
    auto len  = s.size();
    auto slen = sub.size();
    if (len < slen)
    {
        return false;
    }
    return s.compare(0,slen,sub) == 0;
}


bool tmms::base::StringUtils::EndWith(const std::string& s, const std::string& sub)
{
    if (sub.empty())
    {
        return true ;
    }
    if (s.empty())
    {
        return false;
    }
    auto len  = s.size();
    auto slen = sub.size();
    if (len < slen)
    {
        return false;
    }
    return s.compare(len - slen,slen,sub) == 0;
}


std::string tmms::base::StringUtils::FilePath(const std::string& path)
{
    auto pos = path.find_last_of("/\\");
    if (pos != std::string::npos)
    {
        return path.substr(0,pos);
    }else
    {
        return "./";
    }
}

std::string tmms::base::StringUtils::FileNameExt(const std::string &path)
{
    auto pos = path.find_last_of("/\\");
    if (pos != std::string::npos)
    {
      if (pos+1 < path.size() )
      {
        return path.substr(pos+1);
      }
    }else
    {
        return "./";
    }
        return path;
}


std::string  tmms::base::StringUtils::FileName(const std::string &path)
{
    std::string file_name =  path;
    auto pos = path.find_last_of(".");
    if (pos != std::string::npos)
    {
        if (pos!=0)
        {
            return file_name.substr(0,pos);
        }

    }
    return file_name;
}


std::string  tmms::base::StringUtils::Extension(const std::string &path)
{
    const std::string& file_name =  path;
    auto pos = path.find_last_of('.');
    if (pos != std::string::npos)
    {
        if (pos != 0  && pos+1 <file_name.size())
        {
            return file_name.substr(0,pos);
        }
    }
    return std::string();
}

std::vector<std::string> tmms::base::StringUtils::SplitString(const std::string &s, const std::string  &delimiter)
{
    if (delimiter.empty())
    {
        return std::vector<std::string> {};
    }
    std::vector<std::string> result;
    size_t last = 0;
    size_t next =0 ;
    while ((next =s.find(delimiter,last))!= std::string::npos)
    {
        if (next>last)
        {
            result.emplace_back(s.substr(last,next - last));
        }
        last = next + delimiter.size();
    }
    if (last < s.size())
    {
        result.emplace_back(s.substr(last));
    }
    return result;
}


std::vector<std::string> tmms::base::StringUtils::SplitStringWithFSM(const std::string &s, const char &delimiter)
{
    enum
    {
        KStateInit =0,
        KStateNormal = 1,
        KStateDelimiter = 2,
        KStateEnd =3,
    };
    std::vector<std::string> result;
    int state = KStateInit;

    state = KStateNormal;
    std::string tmp;

    for(int pos =0 ;pos <s.size();)
    {
        if(state ==KStateNormal)
        {
            if(s.at(pos)==delimiter ) // 等于界定符，改变状态机的状态
            {
                state  =  KStateDelimiter;
                continue;
            }
            tmp.push_back(s.at(pos));
            pos ++;
        }else if(state ==  KStateDelimiter)
        {
            result.push_back(tmp);
            tmp.clear();
            state = KStateNormal;
            pos++;
        }
    }
    if(!tmp.empty())
    {
        result.push_back(tmp);
    }
    return result;


    


}  